var grid = "";

$(function() {
	var exportBtn = $("#exportBtn").val(); //导出权限按钮控制
	var viewCheck = $("#viewCheck").val(); //表格权限按钮控制
	var dtGridColumns = [ {
		id : 'id',
		title : '编号',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'mobile',
		title : '用户名',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'real_name',
		title : '借款人姓名',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'yinghuanBenxi',
		title : '应还本息',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'period_num',
		title : '还款期数',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'orderCreateTime',
		title : '发起还款日期',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'expect_repay_time',
		title : '预期还款日期',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'settleDay',
		title : '到账日期',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'faxi',
		title : '逾期罚息',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'fee',
		title : '代扣手续费',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	},{
		id : 'shishouAmount',
		title : '实收总额',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'statusDesc',
		title : '还款状态',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;'
	}, {
		id : 'isConfirm',
		title : '是否确认到账',
		type : 'string',
		columnClass : 'text-center',
		headerStyle : 'color:grey;',
		resolution : function(value, record, column, grid, dataNo, columnNo) {
			if('yes' == record.isConfirm){
				return "已确认";
			}else{
				return "未确认";
			}
		}
	},{
		id : 'operation',
		title : '操作',
		type : 'string',
		columnClass : 'text-center tpl-table-black-operation',
		headerStyle : 'color:grey;',
		resolution : function(value, record, column, grid, dataNo, columnNo) {
	        var content = '';
	        if('还款中'==record.statusDesc || '正常还款扣款失败'==record.statusDesc || '提前还款扣款失败'==record.statusDesc
	        		|| '逾期未还'==record.statusDesc|| '逾期还款扣款失败'==record.statusDesc){
	        	content += '<a href="#" onclick="offlineRepay(\''+record.id+'\');"> <i class="am-icon-edit"></i>线下还款</a> ';
	        }
	        return content;
		}
	}];
	
	var dtGridOption = {
		lang : 'zh-cn',
		ajaxLoad : true,
		check : true,
		checkWidth : '37px',
		myExport : true,
		tools : exportBtn?'export[excel]':'',
		exportURL:'repayment/allRepayListExport',
		loadURL : 'repayment/allRepayList',
		columns : dtGridColumns,
		gridContainer : 'dtGridContainer',
		toolbarContainer : 'dtGridToolBarContainer',
		pageSize : 20,
		pageSizeLimit : [ 10, 20, 30 ]
	};
	grid = $.fn.dlshouwen.grid.init(dtGridOption);
	grid.load();
	$('.form-datetime-lang').datepicker({
	    language:  'zh-CN',
	    format: 'yyyy-mm-dd',
	    autoclose: true
	});
});

function search() {
	  grid.pager.startRecord = 0;
	  grid.pager.nowPage = 1;
	grid.parameters = new Object();
	grid.parameters['mobile'] = $("#mobile").val();
	grid.parameters['realName'] = $("#realName").val();
	grid.parameters['status'] = $("#status").val();
	grid.parameters['confirmStatus'] = $("#confirmStatus").val();
	grid.parameters['startTime'] = $("#startTime").val();
	grid.parameters['endTime'] = $("#endTime").val();
	grid.refresh(true);
}

var index;
function offlineRepay(repaymentId){
    index =	layer.open({
      	  title:'线下还款',
      	  type: 1,
      	  skin: 'layui-layer-rim', //加上边框
      	  area: ['680px', '435px'], //宽高
      	  content: getTable(repaymentId)
      	});
}

function getTable(repaymentId){
	var ary = [];
	ary.push('<div class="am-u-sm-12 am-u-md-12 am-u-lg-12">');
	ary.push('<div class="widget am-cf">');
	ary.push('<form class="am-form am-form-horizontal" id="offlineForm">');
	
	ary.push('<div class="am-form-group">');
	ary.push('<label class="am-u-lg-3 am-form-label"></label>');
	ary.push('<div class="am-u-lg-9">');
	ary.push('<div class="am-u-sm-4"><input name="repayType" type="radio" value="repayment" onchange="getRepayDetail('+repaymentId+')"/>按期还款</div>');
	ary.push('<div class="am-u-sm-5 am-u-end"><input name="repayType" type="radio" value="loan" onchange="getRepayDetail('+repaymentId+')"/>按笔还款</div>');
	ary.push('</div>');
	ary.push('</div>');
	
	ary.push('<div class="am-form-group">');
	ary.push('<label class="am-u-lg-3 am-form-label"></label>');
	ary.push('<div class="am-u-lg-9">');
	ary.push('<div class="am-u-sm-4 am-u-end">应还本金</div>');
	ary.push('<div class="am-u-sm-5 am-u-end" id="yinghuanBj">0</div>');
	ary.push('</div>');
	ary.push('</div>');

	ary.push('<div class="am-form-group">');
	ary.push('<label class="am-u-lg-3 am-form-label"></label>');
	ary.push('<div class="am-u-lg-9">');
	ary.push('<div class="am-u-sm-4 am-u-end">应还利息</div>');
	ary.push('<div class="am-u-sm-5 am-u-end" id="yinghuanLx">0</div>');
	ary.push('</div>');
	ary.push('</div>');

	ary.push('<div class="am-form-group">');
	ary.push('<label class="am-u-lg-3 am-form-label"></label>');
	ary.push('<div class="am-u-lg-9">');
	ary.push('<div class="am-u-sm-4 am-u-end">逾期罚息</div>');
	ary.push('<div class="am-u-sm-5 am-u-end" id="yuqiFaxi">0</div>');
	ary.push('</div>');
	ary.push('</div>');

	ary.push('<div class="am-form-group">');
	ary.push('<label class="am-u-lg-3 am-form-label"></label>');
	ary.push('<div class="am-u-lg-9">');
	ary.push('<div class="am-u-sm-4 am-u-end">应还总额</div>');
	ary.push('<div class="am-u-sm-5 am-u-end" id="yinghuanZe">0</div>');
	ary.push('</div>');
	ary.push('</div>');
	
	ary.push('<div class="am-form-group">');
	ary.push('<label class="am-u-lg-3 am-form-label"></label>');
	ary.push('<div class="am-u-lg-9">');
	ary.push('<div class="am-u-sm-4 am-u-end">平账金额</div>');
	ary.push('<div class="am-u-sm-5 am-u-end"><input id="subAmount" type="text" placeholder="多还正值，少还负值"/></div>');
	ary.push('</div>');
	ary.push('</div>');

	ary.push('<div class="am-form-group">');
	ary.push('<label class="am-u-lg-5 am-form-label"></label>');
	ary.push('<div class="am-u-lg-7">');
	ary.push('<button type="button" class="am-btn am-btn-secondary" onclick="offlineRepaySubmit('+repaymentId+')" >提交</button>');
	ary.push('</div>');
	ary.push('</div>');
	
	ary.push('</form>');
	ary.push('</div>');
	ary.push('</div>');
	
	return ary.join("");
}
var flag=false;
function offlineRepaySubmit(repaymentId){
	if(flag){
	      layer.msg('请勿重复点击', { icon : 2 });
	      return;
	}
	var repayType = $("input[name='repayType']:checked").val();
	var subAmount =$("#subAmount").val();
	if(!repayType){
      layer.msg('请选择还款方式', { icon : 2 });
      return;
	}
	if(!subAmount){
		layer.msg('请输入平账金额,实还金额等于应还总额输入0', { icon : 2	});
		return;
	}
	if($("#yinghuanZe").html()==0){
		layer.msg('获取到应还总额后再提交', { icon : 2 });
		return;
	}
	flag=true;
	$.ajax({
        type : "post",
        url :  "repayment/offlineRepay",
        dataType : "json",
        data : {"repayType":repayType,"repaymentId":repaymentId,"subAmount":subAmount},
        success : function(resultdata) {
        	flag=false;
            if (resultdata.ret=="200" && resultdata.content && resultdata.content.code=="10000") {
            	layer.msg("还款成功！", { icon : 1});
            	layer.close(index);
            } else {
                layer.msg(resultdata.message, { icon : 3});
            }
        },
        error : function(errorMsg) {
        	flag=false;
            layer.msg('服务器未响应,请稍后再试', {icon : 3});
        }
    });
}

function getRepayDetail(repaymentId){
	var repayType = $("input[name='repayType']:checked").val();
	$.ajax({
        type : "post",
        url :  "repayment/getRepayDetail",
        dataType : "json",
        data : {"repayType":repayType,"repaymentId":repaymentId},
        success : function(resultdata) {
            if (resultdata.ret=="200") {
            	$("#yinghuanBj").html(resultdata.content.info.yinghuanBj);
            	$("#yinghuanLx").html(resultdata.content.info.yinghuanLx);
            	$("#yuqiFaxi").html(resultdata.content.info.yuqiFaxi);
            	$("#yinghuanZe").html(resultdata.content.info.yinghuanZe);
            } else {
                layer.msg(resultdata.message, { icon : 3});
            }
        },
        error : function(errorMsg) {
            layer.msg('服务器未响应,请稍后再试', {icon : 3});
        }
    });
}

